package ModelImplementation

import (
	"SPC/Model"
	"time"
)

type UnionAffiliation struct {
	itsMemberId       int
	itsDues           float64
	itsServiceCharges map[time.Time]ServiceCharge
}

func NewUnionAffliliation(memberId int, dues float64) *UnionAffiliation {
	return &UnionAffiliation{
		memberId,
		dues,
		make(map[time.Time]ServiceCharge),
	}
}

//用一个全局变量来模拟静态变量，所有p操作都是在操作它
var p *UnionAffiliation = &UnionAffiliation{
	itsMemberId: 0,
	itsDues: 0,
	itsServiceCharges: make(map[time.Time]ServiceCharge),
}

//获取该全局对象
func GetUnionAffiliation() *UnionAffiliation {
	return p
}

func (u *UnionAffiliation) GetServiceCharge(date time.Time) ServiceCharge {
	return u.itsServiceCharges[date]
}

func (u *UnionAffiliation) AddServiceCharge(date time.Time, amount float64) {
	sc := NewServiceCharge(date, amount)
	u.itsServiceCharges[date] = *sc
}

func (u *UnionAffiliation) GetDues() float64 {
	return u.itsDues
}

func (u *UnionAffiliation) GetMemberId() int {
	return u.itsMemberId
}

func (u *UnionAffiliation) CalculateDeductions(pc Model.Paycheck) float64 {
	totalServiceCharge := 0.0
	totalDues := 0.0

	for k, _ := range u.itsServiceCharges {
		sc := u.itsServiceCharges[k]
		dt1 := pc.GetPayPeriodStartDate()
		dt2 := pc.GetPayPeriodEndDate()
		if (k.After(dt1) && k.Before(dt2)) {
			totalServiceCharge += sc.GetAmount()
		}
	}

	fridays := pc.GetPayPeriodStartDate()
	totalDues = u.itsDues * float64(fridays.Day())

	return totalDues + totalServiceCharge
}

func (u *UnionAffiliation) NumberOfFridaysInPayPeriod(payPeriodStart time.Time, payPeriodEnd time.Time) int {
	fridays := 0;
	fridays += payPeriodStart.Day()-payPeriodEnd.Day()
	return fridays
}